import 'package:vamos/core/models/chat_message_entity.dart';

/// 聊天会话数据模型
/// 包含会话的基本信息和参与者
class ChatConversationEntity {
  final String conversationId;
  final String participantId;
  final String participantName;
  final String participantAvatar;
  final String lastMessageContent;
  final ChatMessageType lastMessageType;
  final DateTime lastMessageTime;
  final List<ChatMessageEntity> messageList;

  const ChatConversationEntity({
    required this.conversationId,
    required this.participantId,
    required this.participantName,
    required this.participantAvatar,
    required this.lastMessageContent,
    required this.lastMessageType,
    required this.lastMessageTime,
    this.messageList = const [],
  });

  /// 从JSON创建会话对象
  factory ChatConversationEntity.fromJson(Map<String, dynamic> json) {
    return ChatConversationEntity(
      conversationId: json['conversationId'] as String,
      participantId: json['participantId'] as String,
      participantName: json['participantName'] as String,
      participantAvatar: json['participantAvatar'] as String,
      lastMessageContent: json['lastMessageContent'] as String,
      lastMessageType: ChatMessageType.values.firstWhere(
        (e) => e.toString() == 'ChatMessageType.${json['lastMessageType']}',
      ),
      lastMessageTime: DateTime.parse(json['lastMessageTime'] as String),
      messageList:
          (json['messageList'] as List<dynamic>?)
              ?.map(
                (e) => ChatMessageEntity.fromJson(e as Map<String, dynamic>),
              )
              .toList() ??
          [],
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'conversationId': conversationId,
      'participantId': participantId,
      'participantName': participantName,
      'participantAvatar': participantAvatar,
      'lastMessageContent': lastMessageContent,
      'lastMessageType': lastMessageType.toString().split('.').last,
      'lastMessageTime': lastMessageTime.toIso8601String(),
      'messageList': messageList.map((e) => e.toJson()).toList(),
    };
  }

  /// 复制会话对象并修改部分属性
  ChatConversationEntity copyWith({
    String? conversationId,
    String? participantId,
    String? participantName,
    String? participantAvatar,
    String? lastMessageContent,
    ChatMessageType? lastMessageType,
    DateTime? lastMessageTime,
    List<ChatMessageEntity>? messageList,
  }) {
    return ChatConversationEntity(
      conversationId: conversationId ?? this.conversationId,
      participantId: participantId ?? this.participantId,
      participantName: participantName ?? this.participantName,
      participantAvatar: participantAvatar ?? this.participantAvatar,
      lastMessageContent: lastMessageContent ?? this.lastMessageContent,
      lastMessageType: lastMessageType ?? this.lastMessageType,
      lastMessageTime: lastMessageTime ?? this.lastMessageTime,
      messageList: messageList ?? this.messageList,
    );
  }

  @override
  String toString() {
    return 'ChatConversationEntity(conversationId: $conversationId, participantName: $participantName, lastMessageContent: $lastMessageContent)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is ChatConversationEntity &&
        other.conversationId == conversationId;
  }

  @override
  int get hashCode => conversationId.hashCode;
}
